//speech_client
//1.进行服务发现--发现speech_client的服务节点地址信息并实例化的通信信道
//2.读取语音数据
//3.发起语音rpc调用
#include"../../../speech/aip-cpp-sdk-4.16.7/speech.h"
#include"../../common/etcd.hpp"
#include<gflags/gflags.h>
#include"speech.pb.h"
#include"../../common/channel.hpp"
#include<thread>

DEFINE_bool(run_mode,false,"程序运行的模式,false-调试,true-发布");
DEFINE_string(log_file,"","发布模式下,用于指定日志输出的文件");
DEFINE_int32(log_level,0,"发布模式下,用于指定日志输出等级");

DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(base_service,"/service","服务监控根目录");
DEFINE_string(speech_service,"/service/speech_service","服务监控根目录");

int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc,&argv,true);
    wzl::init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    //1.先构造PRC信道管理对象
    auto service_manager = std::make_shared<ServiceManager>();
    service_manager->declared(FLAGS_speech_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline,service_manager.get()\
    ,std::placeholders::_1,std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline,service_manager.get()\
    ,std::placeholders::_1,std::placeholders::_2);
    //2.构造服务发现对象
    Discovery::ptr dclient = std::make_shared<Discovery> (FLAGS_etcd_host,FLAGS_base_service,put_cb,del_cb);

    //3.通过RPC信道管理对象,获取提供Echo服务的信道
    auto channel = service_manager->choose(FLAGS_speech_service);
    if(!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return -1;
    }
    //读取语音文件数据
    std::string file_content;
    aip::get_file_content("16k.pcm", &file_content);
    std::cout << file_content.size() << std::endl;
    std::cout<<"sss"<<std::endl;
    wzl::SpeechService_Stub stub(channel.get());
    wzl::SpeechRecognitionReq req;
    req.set_speech_content(file_content);
    req.set_request_id("111111");

    wzl::SpeechRecognitionRsp* resp = new wzl::SpeechRecognitionRsp();
    brpc::Controller* cntl = new brpc::Controller();
    stub.SpeechRecognition(cntl,&req,resp,nullptr);
    if(cntl->Failed() == true)
    {
        std::cout<<"Rpc调用失败"<<cntl->ErrorText()<<std::endl;
        delete cntl;
        delete resp;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return -1;
    }
    if(resp->success() == false)
    {
        std::cout<<resp->errmsg()<<std::endl;
        return -1;
    }
    std::cout<<"收到响应"<<resp->request_id()<<std::endl;
    std::cout<<"收到响应"<<resp->recognition_result()<<std::endl;
    
    return 0;
}
